package com.myosw.quartz;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.transaction.file.FileResourceManager;
import org.apache.commons.transaction.file.ResourceManagerException;
import org.apache.commons.transaction.util.CommonsLoggingLogger;
import org.apache.commons.transaction.util.LoggerFacade;
import org.quartz.Job;
import org.quartz.JobDetail;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SimpleTrigger;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.myosw.dao.UrlsDao;
import com.myosw.dao.WebPageDao;
import com.myosw.jdbc.common.DBManager;
import com.myosw.pojo.Nutchlog;
import com.myosw.pojo.Urls;

public class NutchJob implements Job {

	private static Logger _log = LoggerFactory.getLogger(NutchJob.class);

	@Override
	public void execute(JobExecutionContext jobcontext) throws JobExecutionException {
	
			UrlsDao urlsDao = new UrlsDao();
			 List<Urls> list = urlsDao.getRunUrls();
			 
			_updateFile(list);
			_runShell();
			_updateData(list);
			
		//关闭数据库链接
		DBManager.closeConnection(); //释放数据库连接到连接池中
	}

	
	/**
	 * 执行linux Shell命令
	 */
	private void _runShell()
	{
		_log.info("执行linux 中的shell 命令{}", new Date());

		try {
			//取得Nutch安装目录
			PropertiesConfiguration quartzProperties = new PropertiesConfiguration("system.properties");
			String nutchPath= quartzProperties.getString("nutchPath");
			Process process = null;
			String command = nutchPath+"/run.sh";
			_log.info("修改文件的运行权限：{}",command);
			Runtime.getRuntime().exec("chmod 777 "+command);//修改文件权限
			_log.info("执行Shell命令：{}",command);
			process = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c",command},null,null);
			if (process != null) {
					_log.info("执行{}进程",process.toString());
					_log.info(process.getOutputStream().toString());
				
				  InputStreamReader ir = new InputStreamReader(process  
			                .getInputStream());  
			        LineNumberReader input = new LineNumberReader(ir);  
			        String line;  
			        process.waitFor();  
			        while ((line = input.readLine()) != null){  
			        	_log.info(line);  
			        }  
				
			}
		} catch (ConfigurationException | IOException | InterruptedException e) {
			_log.error("执行Shell命令失败！");
			e.printStackTrace();
		}
		
		_log.info("完成linux 中的shell 命令{}", new Date());
	}
	
	
	private void _updateFile(List<Urls> urls) {
		String nutchPath = null;
		String solrUrl =null;
		int depth =10;
		int topN = 10;
		int	threads = 10;
		try {
			PropertiesConfiguration quartzProperties = new PropertiesConfiguration("system.properties");
			nutchPath= quartzProperties.getString("nutchPath");
			solrUrl= quartzProperties.getString("solrUrl");
			depth = quartzProperties.getInt("depth");
			topN = quartzProperties.getInt("topN");
			threads =quartzProperties.getInt("threads");
		} catch (ConfigurationException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		
		File filesend = new File(nutchPath+"/temp/sendtest.txt");
		//如果文件不存在就创建
		if(!filesend.exists()){
			try {
				filesend.createNewFile();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		CharSequence sendstring = "";//设置爬虫网址
		
		for (Urls url : urls) {
			sendstring = sendstring+url.getUrl()+"\n";
		}
		_log.info(sendstring.toString());
		try {
			FileUtils.write(filesend, sendstring , "UTF-8");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		File filerun = new File(nutchPath+"/temp/runtest.sh");
		//如果文件不存在就创建
		if(!filerun.exists()){
			try {
				filerun.createNewFile();
				filerun.setWritable(true);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		
		CharSequence runstring = nutchPath+"/bin/nutch crawl "+nutchPath+"/urls -depth "+depth+" -topN "+topN+" -threads "+threads+"\n"
				+nutchPath+"/bin/nutch solrindex "+solrUrl+"  -reindex ";
		try {
			FileUtils.write(filerun, runstring , "UTF-8");
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		String storeDir = nutchPath;//存储路径
		String workDir = "temp";//临时路径
		Log log =LogFactory.getLog(NutchJob.class);
		LoggerFacade logger = new CommonsLoggingLogger(log) ;
		FileResourceManager frmanager = new FileResourceManager(storeDir, workDir, false, logger);
		try {
			frmanager.start();//启动线程
			String txId=frmanager.generatedUniqueTxId();
			try {
			
				frmanager.startTransaction(txId);//启动事物处理
				frmanager.copyResource(txId, "/temp/sendtest.txt", "urls/send.txt", true);
				frmanager.copyResource(txId, "/temp/runtest.sh", "run.sh", true);
				frmanager.commitTransaction(txId);
			} catch (ResourceManagerException e) {
				frmanager.rollbackTransaction(txId);
				e.printStackTrace();
			}
		
			
		} catch (ResourceManagerException e) {
			//frmanager.rollbackTransaction(txId);
			e.printStackTrace();
		}
	}
	
	
	private void _updateData(List<Urls> list)
	{
		
		WebPageDao webPageDao = new WebPageDao();
		for (Urls urls : list) 
		{
			
			int count =webPageDao.getCount(urls.getUrl());
			int newcount=count-urls.getTotal();
			Urls bean = new Urls();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("total", count);
			map.put("newcount",newcount );
			map.put("updatetime", new Timestamp(new Date().getTime()));
			bean.update(map , urls.getId());//更新
			
			Nutchlog nutchlog = new Nutchlog(); //添加更新日志
			nutchlog.setUrls(urls.getId());
			nutchlog.setNewcount(newcount);
			nutchlog.save();
		}
	}
	
}
